package org.dei.perla.utils;

public abstract class Observer implements Runnable, ObserverInterface{
	
	private Notifier notifier;
	
	/* (non-Javadoc)
	 * @see org.dei.perla.utils.ObserverInterface#whenWokedUp()
	 */
	public abstract void whenWokedUp();
	
	/* (non-Javadoc)
	 * @see org.dei.perla.utils.ObserverInterface#startToOberve(org.dei.perla.utils.Notifier)
	 */
	public void startToOberve(Notifier notifier){
		this.notifier = notifier;
	}
	
	public void run(){
		try {
			while(true){
				synchronized (notifier) {
					do {
						notifier.wait();
					}while(!notifier.isTheTurnOf(this));
				} 
				this.whenWokedUp();
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
}
